package com.yl.order.db;

import android.content.Context;
import android.database.Cursor;

import org.greenrobot.greendao.database.Database;

/**
 * Created by Administrator on 2018/5/16 0016.
 * 数据库管理
 */
public class DbManage {
    public static final String DB_NAME = "tea_order.db";
    private static DbManage mInstance;
    //    private Database database;
    private DaoMaster mDaoMaster;
    private DaoSession mDaoSession;

    private boolean hasInit = false;

    public static DbManage getInstance() {
        if (mInstance == null) {
            synchronized (DbManage.class) {
                if (mInstance == null) {
                    mInstance = new DbManage();
                }
            }
        }
        return mInstance;
    }

    public void init(Context context, String path) {//app的context
        //Database database = new DaoOpenHelper(new GreenDaoContext(App.getInstance().getApplicationContext()), DB_NAME).getWritableDb();
        //不指定路径
        if (!hasInit) {
            Database database = new DaoOpenHelper(context.getApplicationContext(), path, DB_NAME).getWritableDb();
            mDaoMaster = new DaoMaster(database);
            mDaoSession = mDaoMaster.newSession();
            hasInit = true;
        }
    }

    public boolean isInit() {
        return hasInit;
    }

    private DbManage() {

    }


    public DaoSession getDaoSession() {
        mDaoSession.clear();
        return mDaoSession;
    }

    public void executeSql(String sql) {
        mDaoMaster.getDatabase().execSQL(sql);
    }

    public Cursor rawQuery(String sql, String[] selectionArgs) {
        return mDaoMaster.getDatabase().rawQuery(sql, selectionArgs);
    }
}
